﻿using System;
using System.Web;

namespace DuJianliang.ApplicationPlatform.Common
{
    /// <summary>
    /// 用户身份授权验证
    /// </summary>
    public class UserAuthorizationModule : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }

        void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication httpApplication = (HttpApplication)sender;
            HttpContext httpContext = httpApplication.Context;

            // 检查用户是否已经登录
            if (httpContext.Session != null && CurrentInfo.Instance.CurrentUser == null)
            {
                // 获取Url
                string requestUrl = httpApplication.Request.Url.ToString();

                // 获取请求的页面
                string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);

                // 如果请求的页面不是登录页面，刚重定向到登录页面。
                if (!requestPage.ToLower().Equals("login.aspx"))
                {
                    httpContext.Response.Redirect("login.aspx");
                }
            }
        }

    }
}